package com.example.administrator.yanfoxconn.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.yanfoxconn.R;
import com.example.administrator.yanfoxconn.activity.JIListActivity;
import com.example.administrator.yanfoxconn.activity.KOListActivity;
import com.example.administrator.yanfoxconn.activity.LRPalletListActivity;
import com.example.administrator.yanfoxconn.activity.LRSearchListActivity;
import com.example.administrator.yanfoxconn.activity.YJExceListActivity;
import com.example.administrator.yanfoxconn.bean.DNReform;
import com.example.administrator.yanfoxconn.bean.JIMessage;
import com.example.administrator.yanfoxconn.bean.KOInfo;
import com.example.administrator.yanfoxconn.bean.LRMessage;
import com.example.administrator.yanfoxconn.bean.YJExceMessage;

import java.util.List;

/**
 * Created by Song
 * on 2020/10/20
 * Description：宿舍查驗 異常整改item
 *              JI辦事處車輛預約
 *              JQ營建異常提交及整改
 *              KO安保部 臨時來訪
 */
public class DNReformAdapter extends BaseAdapter {
    public Context mContext;
    private List<DNReform> lists;
    public JIListActivity jiListActivity;
    private List<JIMessage> jiMessages;
    private YJExceListActivity yjExceListActivity;
    private List<YJExceMessage> yjExceMessagesList;
    private KOListActivity koListActivity;
    private List<KOInfo> koInfoList;
    private List<LRMessage> lrSearchList;
    private String from="",flag="";

    private OnClickListenerSeeOrAdd onClickListenerSeeOrAdd;
    public DNReformAdapter(Context context, List<DNReform> lists,String from) {
        this.mContext = context;
        this.lists = lists;
        this.from = from;

    }
    public DNReformAdapter(JIListActivity jiListActivity, List<JIMessage> lists,String from) {
        this.jiListActivity = jiListActivity;
        this.jiMessages = lists;
        this.from = from;
    }
    public DNReformAdapter(YJExceListActivity yjExceListActivity, List<YJExceMessage> yjExceMessagesList,String from) {
        this.yjExceListActivity = yjExceListActivity;
        this.yjExceMessagesList = yjExceMessagesList;
        this.from = from;
    }
    public DNReformAdapter(KOListActivity koListActivity, List<KOInfo> koInfoList,String from) {
        this.koListActivity = koListActivity;
        this.koInfoList = koInfoList;
        this.from = from;
    }
    public DNReformAdapter(KOListActivity koListActivity, List<KOInfo> koInfoList,String from,String flag) {
        this.koListActivity = koListActivity;
        this.koInfoList = koInfoList;
        this.from = from;
        this.flag = flag;
    }

    public DNReformAdapter(LRSearchListActivity lrSearchListActivity, List<LRMessage> lrSearchList, String from, String flag) {
        this.mContext = lrSearchListActivity;
        this.from = from;
        this.flag = flag;
        this.lrSearchList = lrSearchList;
    }
    public DNReformAdapter(LRPalletListActivity lrPalletListActivity, List<LRMessage> lrSearchList, String from, String flag) {
        this.mContext = lrPalletListActivity;
        this.from = from;
        this.flag = flag;
        this.lrSearchList = lrSearchList;
    }

    @Override
    public int getCount() {

        if (from.equals("JI")){
            return jiMessages.size();
        }else if (from.equals("JQ")||from.equals("JQEND")){
            return yjExceMessagesList.size();
        }else if (from.equals("KO")){
            return koInfoList.size();
        }else if (from.equals("LR")||from.equals("QRP")){
            return lrSearchList.size();
        }else{
            return lists.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (from.equals("JI")){
            return jiMessages.get(position);
        }else if (from.equals("JQ")||from.equals("JQEND")){
            return yjExceMessagesList.get(position);
        }else if (from.equals("ko")){
            return koInfoList.get(position);
        }else if (from.equals("LR")||from.equals("QRP")){
            return lrSearchList.get(position);
        }else{
            return lists.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            Log.e("===", "ss");
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_dn_reform_lv, null);
            holder.tvNo = (TextView)convertView.findViewById(R.id.tv_no);
            holder.tvRoom = (TextView)convertView.findViewById(R.id.tv_room);
            holder.tvBed = (TextView)convertView.findViewById(R.id.tv_bed);
            holder.tvName = (TextView)convertView.findViewById(R.id.tv_name);
            holder.tvDate = (TextView)convertView.findViewById(R.id.tv_date);
            holder.tvResult = (TextView)convertView.findViewById(R.id.tv_result);
            holder.tvSee = (TextView)convertView.findViewById(R.id.tv_see);
            holder.llOne = (LinearLayout) convertView.findViewById(R.id.ll_one);
            holder.llLR = (LinearLayout) convertView.findViewById(R.id.ll_lr);
            holder.llQRP = (LinearLayout) convertView.findViewById(R.id.ll_qrp);
            holder.tvlrNo = (TextView)convertView.findViewById(R.id.tv_lr_no);
            holder.tvCarNum = (TextView)convertView.findViewById(R.id.tv_car_num);
            holder.tvArea = (TextView)convertView.findViewById(R.id.tv_area);
            holder.tvPllNum = (TextView)convertView.findViewById(R.id.tv_pall_num);
            holder.tvstatue = (TextView)convertView.findViewById(R.id.tv_statue);
            holder.tv_pll_no = (TextView)convertView.findViewById(R.id.tv_pll_no);
            holder.tv_fh = (TextView)convertView.findViewById(R.id.tv_fh);
            holder.tv_sh = (TextView)convertView.findViewById(R.id.tv_sh);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (from.equals("JI")){
            holder.tvNo.setText("" + (position + 1));
            holder.tvRoom.setText(jiMessages.get(position).getDriver_num());
            holder.tvRoom.setTextSize(16);
            holder.tvBed.setVisibility(View.GONE);
            holder.tvName.setText(jiMessages.get(position).getDriver_name());
            holder.tvDate.setText(jiMessages.get(position).getEnd_date());

            holder.tvResult.setText(jiMessages.get(position).getStart_address());
//        Log.e("-----------","lists.get(position).getJc_result().get(0).getName()==="+lists.get(position).getJc_result().get(0).getName());
            holder.tvSee.setText("放行");
        }else if (from.equals("KO")){
            holder.tvNo.setText("" + (position + 1));
            holder.tvRoom.setText(koInfoList.get(position).getC_DATE());
            holder.tvBed.setVisibility(View.GONE);
            holder.tvName.setText(koInfoList.get(position).getC_NAME());
            holder.tvDate.setText(koInfoList.get(position).getC_CAR());

            if (flag.equals("search")){

                holder.tvResult.setText(koInfoList.get(position).getC_DEP()+"-->"+koInfoList.get(position).getM_DEP());
            }else{
            holder.tvResult.setText(koInfoList.get(position).getM_MENG());
            }

//        Log.e("-----------","lists.get(position).getJc_result().get(0).getName()==="+lists.get(position).getJc_result().get(0).getName());
            holder.tvSee.setText("放行");
        }else if (from.equals("JQ")||from.equals("JQEND")){
            holder.tvNo.setText("" + (position + 1));
            holder.tvRoom.setText(yjExceMessagesList.get(position).getYj_building());
            holder.tvBed.setText(yjExceMessagesList.get(position).getYj_position());
            if (yjExceMessagesList.get(position).getExce_statue().equals("0")){
                holder.tvNo.setBackgroundColor(yjExceListActivity.getResources().getColor(R.color.color_d73d19));
                holder.tvName.setText("未處理");
            }else if(yjExceMessagesList.get(position).getExce_statue().equals("1")){
                holder.tvNo.setBackgroundColor(yjExceListActivity.getResources().getColor(R.color.color_00CCFF));
                holder.tvName.setText("處理中");
            }else if(yjExceMessagesList.get(position).getExce_statue().equals("2")){
                holder.tvNo.setBackgroundColor(yjExceListActivity.getResources().getColor(R.color.color_42D42B));
                holder.tvName.setText("處理完成");
            }else if(yjExceMessagesList.get(position).getExce_statue().equals("3")){

                holder.tvName.setText(yjExceMessagesList.get(position).getExce_statue());
            }
            holder.tvResult.setText(yjExceMessagesList.get(position).getExce_desp());
            holder.tvDate.setVisibility(View.GONE);
            if (from.equals("JQEND")){
                holder.tvSee.setText("結案");}else{
            holder.tvSee.setText("維護");}
        }else if (from.equals("LR")){
            holder.llOne.setVisibility(View.GONE);
            holder.llLR.setVisibility(View.VISIBLE);
            holder.tvlrNo.setText("" + (position + 1));
            holder.tvCarNum.setText(lrSearchList.get(position).getCARNUM());
            holder.tvCarNum.setTextColor(mContext.getResources().getColor(R.color.design_default_color_primary_dark));
            if (flag.equals("FH")){
                holder.tvArea.setText(lrSearchList.get(position).getRECEIVEAREA());
            }else{
                holder.tvArea.setText(lrSearchList.get(position).getSENDAREA());
            }
            holder.tvPllNum.setText(lrSearchList.get(position).getPALLETNUM());
            holder.tvstatue.setText(lrSearchList.get(position).getSTATUS());

            holder.tvResult.setVisibility(View.GONE);
        }else if (from.equals("QRP")){
            holder.llOne.setVisibility(View.GONE);
            holder.llQRP.setVisibility(View.VISIBLE);
            holder.tv_pll_no.setText(lrSearchList.get(position).getPALLETNUM());
            holder.tv_fh.setText(lrSearchList.get(position).getSENDAREA());
            holder.tv_sh.setText(lrSearchList.get(position).getRECEIVEAREA());
        }else{
            holder.tvNo.setText("" + (position + 1));
            holder.tvRoom.setText(lists.get(position).getJc_room());
            holder.tvBed.setText(lists.get(position).getJc_bed());
            holder.tvName.setText(lists.get(position).getEmp_name());
            holder.tvDate.setText(lists.get(position).getJc_date());
            holder.tvResult.setText(lists.get(position).getJc_result().get(0).getName());
//        Log.e("-----------","lists.get(position).getJc_result().get(0).getName()==="+lists.get(position).getJc_result().get(0).getName());
            holder.tvSee.setText("整改");
        }
        holder.tvSee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListenerSeeOrAdd != null) {
                    onClickListenerSeeOrAdd.OnClickListenerSee(position);
                }
            }
        });
        return convertView;
        }

    private class ViewHolder{
        public TextView tvNo,tvRoom,tvBed,tvName,tvDate,tvResult;
        public TextView tvSee;
        public LinearLayout llOne,llLR,llQRP;
        public TextView tvlrNo,tvCarNum,tvArea,tvPllNum,tvstatue;
        public TextView tv_pll_no,tv_fh,tv_sh;
    }
    public interface OnClickListenerSeeOrAdd {
        void OnClickListenerSee(int position);
    }

    public void setOnClickListenerSeeOrAdd(OnClickListenerSeeOrAdd onClickListenerSeeOrAdd) {
        this.onClickListenerSeeOrAdd = onClickListenerSeeOrAdd;
    }
}
